package com.ksyzt.ksite2.test;

import java.io.File;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ChangeName {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		ChangeName app= new ChangeName();
		String path="C:\\Documents and Settings\\Administrator\\桌面\\ArcGIS Server Object Container Account\\My Documents\\Downloads\\2-3级页面修改\\2-3级页面修改\\";
		File f=new File(path);
		
		File outpath=new File(path+"\\out");
		if(!outpath.exists())
		{
			
		}
		else
		{
		
		}
		outpath.mkdir();
		File[] files=f.listFiles();
		
		System.out.println(files.length);
		for(File file:files)
		{
			if(file.isFile())
			{
			String name=file.getPath();
			System.out.println(name);
			name=app.HanyuToPinyin(name);
			System.out.println(name);
			//String fn=path+"\\out\\"+name;
			//file.renameTo(new File(fn));
			}
		}
	}
	
	private String HanyuToPinyin(String name){
        char[] nameChar = name.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = 
                                           new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String pinyinName="";
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    String pinyin= PinyinHelper.toHanyuPinyinStringArray
                                           (nameChar[i], defaultFormat)[0];
                    pinyinName +=pinyin.substring(0,1);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } 
        }
        return pinyinName;
    }


}
